package com.example.actionsignin.mvp.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.example.actionsignin.R;
import com.example.actionsignin.entity.LoginEntity;
import com.example.actionsignin.http.HttpUtil;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.disposables.Disposables;
import io.reactivex.schedulers.Schedulers;

/**
 * 欢迎界面
 */
public class WelcomeActivity extends AppCompatActivity {

    private static final String TAG = "WelcomeActivity";
    private String mAccount;
    private String mPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        readAccount();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1500);

                    if (!mAccount.equals("") && !mPassword.equals(""))
                    {
                        doLogin(mAccount,mPassword);
//                        toMain();
                    }else {
                        toEntry();
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void readAccount()
    {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("data",MODE_PRIVATE);
        mAccount  = sharedPreferences.getString("account","");
        mPassword = sharedPreferences.getString("password","");
    }

    private void toEntry()
    {
        Intent intent = new Intent(WelcomeActivity.this,EntryActivity.class);
        startActivity(intent);
        finish();
    }

    private void doLogin(String telephone,String password)
    {
        HttpUtil.onSend().onLogin(telephone,password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginEntity>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull LoginEntity loginEntity) {
                        if (loginEntity.getResult().equals("1"))
                        {
                            toMain();
                        }else {
                            toEntry();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d(TAG, "onError: " + e);
                    }

                    @Override
                    public void onComplete() {
                        Disposables.disposed();
                    }
                });
    }

    private void toMain()
    {
        Intent intent = new Intent(WelcomeActivity.this,MainActivity.class);
        startActivity(intent);
        finish();
    }
}
